﻿using Station.Inspect.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Vision.Common.Forms;

namespace Vision.Common.Draw
{
    public partial class UCCameraEdit : UserControl
    {
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<NodeComponent> Components { get; set; } = new List<NodeComponent>();
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<NodeMark> Marks { get; set; } = new List<NodeMark>();
        /// <summary>
        /// 当前操作模式
        /// </summary>
        EditMode curMode = EditMode.None;
        /// <summary>
        /// 当前捕获状态
        /// </summary>
        TrackerInfo CurTracker { get; set; }
        /// <summary>
        /// 鼠标左键按下
        /// </summary>
        bool LMouseIsDown = false;
        /// <summary>
        /// 鼠标位置
        /// </summary>
        PointF LMouseDownPos = default;
        NodeElement DrawRect { get; set; } = null;
        NodeMark DrawMark { get; set; } = null;
        NodeComponent CopyedCompoent { get; set; } = null;
        CDrawObj selectedObj;
        /// <summary>
        /// 当前选中元件
        /// </summary>
        CDrawObj SelectedObj
        {
            get => selectedObj;
            set
            {
                if (selectedObj == value)
                    return;
                if (selectedObj != null)
                    selectedObj.State = TrackerState.normal;
                selectedObj = value;
                if (selectedObj != null)
                    selectedObj.State = TrackerState.active;
                else
                    curMode = EditMode.None;
                if (tvComponent.SelectedNode?.Tag != value)
                {
                    tvComponent.SelectedNode = value?.Tag as TreeNode;
                }
                UpdateLBPos(value);
                ucCanvas.Invalidate();
            }
        }
        
        public UCCameraEdit()
        {
            InitializeComponent();
            UpdateLBPos(SelectedObj);
        }
        private void UCCameraEdit_Load(object sender, EventArgs e)
        {
            
            
        }

        public void ResetComponent(List<NodeComponent> components,List<NodeMark> marks)
        {
            this.Components = components;
            this.Marks = marks;
            ucCanvas.ClearDrawObject();
            tvComponent.Nodes.Clear();
            foreach (var mark in Marks)
                ucCanvas.AddDrawObject(mark);
            foreach (var comp in Components)
            {
                CreateCompNode(comp);
            }
        }

        private void UCCameraEdit_Resize(object sender, EventArgs e)
        {
            toolBar.Height = this.Height;
            ucCanvas.Location = new Point(toolBar.Width, 0);
            ucCanvas.Size = new Size(this.Width - tvComponent.Width- toolBar.Width, this.Height);
            tvComponent.Location = new Point(toolBar.Width + ucCanvas.Width+1, 0);
            tvComponent.Height = this.Height - tbInfo.Height-5;
            tbInfo.Location = new Point(toolBar.Width + ucCanvas.Width+1, this.Height- tbInfo.Height);//12
            //toolBar.Width = this.Width;
            //ucCanvas.Location = new Point(0, toolBar.Height);
            //ucCanvas.Size = new Size(this.Width - tvComponent.Width, this.Height - toolBar.Height);
            //tvComponent.Location = new Point(ucCanvas.Width, toolBar.Height);
            //tvComponent.Height = this.Height - toolBar.Height;
        }

        #region 工具栏
        private void tBtnNewComponent_Click(object sender, EventArgs e)
        {
            curMode = EditMode.CreateComponent;
        }

        private void tBtnNewElement_Click(object sender, EventArgs e)
        {
            curMode = EditMode.CreateElement;
        }
        private void tbMark_Click(object sender, EventArgs e)
        {
            if (Marks.Count >= 2)
            {
                MessageBox.Show("仅支持2个Mark点");
                return;
            }
            curMode = EditMode.CreateMark;
        }
        private void tBtnDelete_Click(object sender, EventArgs e)
        {
            OnDeleteObj();
        }

        private void tBtnRotate_Click(object sender, EventArgs e)
        {
            float angle;
            if (SelectedObj is NodeComponent)
            {
                angle = (SelectedObj as NodeComponent).CurAngle;
            }
            else if (SelectedObj is NodeElement)
            {
                angle = (SelectedObj as NodeElement).Angle;
            }
            else
            {
                MessageBox.Show("请先选中子区或元件");
                return;
            }
            FormRotate formRotate = new FormRotate(angle);
            formRotate.OnUpdateAngle += FormRotate_OnUpdateAngle;
            formRotate.Show();
        }

        private void FormRotate_OnUpdateAngle(object sender, RotateArgs e)
        {
            if (SelectedObj is NodeComponent)
            {
                var selectedComponet = SelectedObj as NodeComponent;
                Matrix matrix = new Matrix();
                float addAngle = e.Angle - selectedComponet.CurAngle;
                matrix.RotateAt(addAngle, selectedComponet.CurPt.Pt);
                foreach (var item in selectedComponet.Elements)
                {
                    PointF[] points = new PointF[1];
                    points[0] = item.StartPoint;
                    matrix.TransformPoints(points);
                    item.StartPoint = points[0];
                    item.Angle += addAngle;
                }
                selectedComponet.CurAngle = e.Angle;
            }
            else if (SelectedObj is NodeElement)
            {

                (SelectedObj as NodeElement).Angle = e.Angle;
            }
            ucCanvas.Invalidate();
        }

        private void tBtnAutoSize_Click(object sender, EventArgs e)
        {
            ucCanvas.AutoSizeImage(false);
            ucCanvas.Invalidate();
        }

        private void tBtnCross_Click(object sender, EventArgs e)
        {
            ucCanvas.ShowCross = !ucCanvas.ShowCross;
            ucCanvas.Invalidate();
        }

        void OnDeleteObj()
        {
            if (SelectedObj == null)
            {
                MessageBox.Show("尚未选中节点");
                return;
            }
            if (SelectedObj is NodeComponent)
            {
                var selectedComp = SelectedObj as NodeComponent;
                Components.Remove(selectedComp);
                ucCanvas.RemoveDrawObject(SelectedObj);
                tvComponent.Nodes.Remove(selectedComp.Tag as TreeNode);
            }
            else if (SelectedObj is NodeElement)
            {
                var selectedElement = SelectedObj as NodeElement;
                NodeComponent nodeComponent = Components.Find(p => p.Elements.Find(s => s == selectedElement) != null);
                if (nodeComponent != null)
                {
                    nodeComponent.RemoveElement(selectedElement);
                    var parNode = nodeComponent.Tag as TreeNode;
                    parNode.Nodes.Remove(selectedElement.Tag as TreeNode);
                }

            }
            else if(SelectedObj is NodeMark)
            {
                var selectedMark = SelectedObj as NodeMark;
                Marks.Remove(selectedMark);
                ucCanvas.RemoveDrawObject(SelectedObj);
            }
            else
            {
                MessageBox.Show("节点类型错误，请检查！");
                return;
            }
            SelectedObj = null;
        }
        #endregion 工具栏

        #region TreeView操作
        private void tvComponent_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                OnRMouseUp(sender as Control, e, false);
            }
        }

        private void tvComponent_AfterSelect(object sender, TreeViewEventArgs e)
        {
            Console.WriteLine(e.Node.FullPath);
            SelectedObj = e.Node.Tag as CDrawObj;
        }
        #endregion TreeView操作

        #region 鼠标操作
        private void ucCanvas_MouseDown(object sender, MouseEventArgs e)
        {
            Console.WriteLine("Camera Down");
            if (e.Button == MouseButtons.Left)
            {
                LMouseIsDown = true;
                LMouseDownPos = ucCanvas.CurPosInImg;
                float curScale = ucCanvas.ScaleCur;
                //TrackerInfo info = default;
                switch (curMode)
                {
                    case EditMode.CreateComponent: break;
                    case EditMode.CreateElement:
                        {
                            var comp = SelectedObj as NodeComponent;
                            if (comp == null)
                            {
                                SelectedObj = TrySelectObj(LMouseDownPos, curScale);
                                comp = SelectedObj as NodeComponent;
                                if (comp == null)
                                {
                                    LMouseIsDown = false;
                                    MessageBox.Show("请先选中子区");
                                }
                            }
                            else
                            {
                                DrawRect = new NodeElement()
                                {
                                    StartPoint = LMouseDownPos,
                                    ID = comp.GetMaxID() + 1,
                                    State = TrackerState.active
                                };
                                comp.AddElement(DrawRect);
                            }
                            break;
                        }
                    case EditMode.CreateMark:
                        {
                            int id = 1;
                            if (Marks.Count > 0)
                            {
                                if (Marks[0].ID == 1)
                                    id = 2;
                                else
                                    id = 1;
                            }
                            DrawMark = new NodeMark()
                            {
                                ID = id,
                                StartPoint = LMouseDownPos,
                                State = TrackerState.active
                            };
                            Marks.Add(DrawMark);
                            ucCanvas.AddDrawObject(DrawMark);
                            break;
                        }
                    default:
                        SelectedObj = TrySelectObj(LMouseDownPos, curScale);
                        break;
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                //cMenu.Items.Clear();
                //if (copyCompoent != null)
                //    cMenu.Items.Add("粘贴子区");
                //var selectedComponet = selectedObj as NodeComponent;
                //if (NodeLibManager.nodeLibs.Count > 0 && selectedComponet != null)
                //{
                //    ToolStripMenuItem menuItem = new ToolStripMenuItem("放置元件");
                //    foreach (var item in NodeLibManager.nodeLibs)
                //    {
                //        if (!selectedComponet.Elements.Exists(p => p.Lib == item))
                //            menuItem.DropDownItems.Add(item.Name);
                //    }
                //    if (menuItem.DropDownItems.Count > 0)
                //    {
                //        menuItem.DropDownItemClicked += cMenuPlace_ItemClicked;
                //        cMenu.Items.Add(menuItem);
                //    }
                //}
                //if (cMenu.Items.Count > 0)
                //    cMenu.Show(ucPreView1, e.Location);
            }
        }

        private void ucCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            switch (curMode)
            {
                case EditMode.CreateComponent:
                case EditMode.CreateElement:
                    if (LMouseIsDown && DrawRect != null)
                    {
                        DrawRect.ReSize(ucCanvas.CurPosInImg);
                        UpdateLBPos(DrawRect);
                    }   
                    break;
                case EditMode.CreateMark:
                    if (LMouseIsDown && DrawMark != null)
                    {
                        DrawMark.ReSize(ucCanvas.CurPosInImg);
                        UpdateLBPos(DrawMark);
                    }
                    break;
                default:
                    if (SelectedObj != null)
                    {
                        if (LMouseIsDown)
                        {
                            if (CurTracker.pos == TrackerPos.tIn)
                            {
                                PointF curPt = ucCanvas.CurPosInImg;
                                selectedObj.Move(curPt.X - LMouseDownPos.X, curPt.Y - LMouseDownPos.Y);
                                LMouseDownPos = curPt;
                                //UpdateLBPos(selectedObj);
                            }
                            else if (CurTracker.pos == TrackerPos.tBorderOne || CurTracker.pos == TrackerPos.tBorderTwo)
                            {
                                var selectedElement = SelectedObj as CDrawRect;
                                if (CurTracker.para != 0 && selectedElement != null)
                                {
                                    selectedElement.ReSize(ucCanvas.CurPosInImg, CurTracker.para);
                                    //UpdateLBPos(selectedObj);
                                }
                                
                            }
                        }
                        else
                        {
                            CurTracker = selectedObj.GetTrack(ucCanvas.CurPosInImg, ucCanvas.ScaleCur);
                            if (CurTracker.pos == TrackerPos.tIn)
                                this.Cursor = Cursors.Hand;
                            else if (CurTracker.pos == TrackerPos.tBorderOne || CurTracker.pos == TrackerPos.tBorderTwo)
                                this.Cursor = Cursors.SizeAll;
                            else
                                this.Cursor = Cursors.Default;
                        }
                    }
                    UpdateLBPos(selectedObj);
                    break;
            }
        }

        private void ucCanvas_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                OnLMouseUp(e);
            }
            else if(e.Button == MouseButtons.Right)
            {
                OnRMouseUp(sender as Control, e);
            }
        }

        void OnLMouseUp(MouseEventArgs e)
        {
            LMouseIsDown = false;
            switch (curMode)
            {
                case EditMode.CreateComponent:
                    NodeComponent component = new NodeComponent()
                    {
                        ID = GetMaxCompID() + 1,
                        BasePt = ucCanvas.CurPosInImg
                    };
                    component.Reset();
                    Components.Add(component);
                    CreateCompNode(component);
                    SelectedObj = component;
                    curMode = EditMode.None;
                    break;
                case EditMode.CreateElement:
                    if (DrawRect != null)
                    {
                        var selectedComponet = SelectedObj as NodeComponent;
                        TreeNode[] items = tvComponent.Nodes.Find(selectedComponet?.Name, false);
                        if (items.Length > 0)
                        {
                            CreateEleNode(items[0], DrawRect);
                        }

                        //SelectedObj = DrawRect;
                        DrawRect = null;
                        curMode = EditMode.None;
                    }
                    break;
                case EditMode.CreateMark:
                    if (DrawMark !=null)
                    {
                        var rect = new Rectangle((int)DrawMark.StartPoint.X, (int)DrawMark.StartPoint.Y, (int)DrawMark.Size.Width, (int)DrawMark.Size.Height);
                        DrawMark.Image = ucCanvas.GetImage(rect, DrawMark.angle);
                        DrawMark = null;
                    }
                    SelectedObj = DrawMark;
                    curMode = EditMode.None;
                    break;
                default:
                    break;
            }
        }
        #endregion 鼠标操作

        #region 右键菜单
        void OnRMouseUp(Control sender, MouseEventArgs e, bool bCreatePlace = true)
        {
            cMenu.Items.Clear();
            var selectedComponet = selectedObj as NodeComponent;
            if (selectedComponet != null)
            {
                cMenu.Items.Add("重命名");
                cMenu.Items.Add("复制子区");
            }
            if (CopyedCompoent != null)
                cMenu.Items.Add("粘贴子区");
            if (selectedComponet != null)
            {
                if (NodeLibManager.nodeLibs.Count > 0 && bCreatePlace)
                {
                    ToolStripMenuItem menuItem = new ToolStripMenuItem("放置元件");
                    foreach (var item in NodeLibManager.nodeLibs)
                    {
                        if (!selectedComponet.Elements.Exists(p => p.Lib == item))
                            menuItem.DropDownItems.Add(item.Name);
                    }
                    if (menuItem.DropDownItems.Count > 0)
                    {
                        menuItem.DropDownItemClicked += cMenu_ItemClicked;
                        cMenu.Items.Add(menuItem);
                    }
                }
                cMenu.Items.Add("删除子区");
            }
            else if (selectedObj is NodeElement)
            {
                var selectedElement = selectedObj as NodeElement;
                cMenu.Items.Add(selectedElement.Lib == null ? "添加至元件库" : "更新元件库");
                if (NodeLibManager.nodeLibs.Count > 0)
                {
                    ToolStripMenuItem menuItem = new ToolStripMenuItem("关联元件库");
                    var nodeComp = Components.Find(p => p.Elements.Contains(selectedElement));
                    foreach (var item in NodeLibManager.nodeLibs)
                    {
                        if (!nodeComp.Elements.Exists(p => p.Lib == item))
                            menuItem.DropDownItems.Add(item.Name);
                    }
                    if (menuItem.DropDownItems.Count > 0)
                    {
                        menuItem.DropDownItemClicked += cMenu_ItemClicked;
                        cMenu.Items.Add(menuItem);
                    }
                }
                cMenu.Items.Add("删除元件");
            }
            else if(selectedObj is NodeMark)
            {
                cMenu.Items.Add("更新算子");
                cMenu.Items.Add("Mark属性");
            }
            if (cMenu.Items.Count > 0)
                cMenu.Show(sender, e.Location);
        }
        private void cMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if(selectedObj is NodeComponent)
            {
                var selectedComponet = selectedObj as NodeComponent;
                switch(e.ClickedItem.Text)
                {
                    case "粘贴子区": break;
                    case "重命名":
                        FormComReName form = new FormComReName();
                        form.CompID = selectedComponet.ID;
                        if (form.ShowDialog()== DialogResult.OK)
                        {
                            NodeComponent com = null;
                            try
                            {
                                com = Components.First(p => p.ID == form.CompID);
                            }
                            catch (Exception ex)
                            {
                            }
                            if (com !=null && com != selectedComponet)
                            {
                                MessageBox.Show($"子区{form.CompID}已经存在");
                            }
                            else
                            {
                                var nodes = tvComponent.Nodes.Find(selectedComponet.Name,false);
                                selectedComponet.ID = form.CompID;
                                for (int i=0;i< nodes.Length;i++)
                                {
                                    nodes[i].Name = selectedComponet.Name;
                                    nodes[i].Text = selectedComponet.Name;
                                }
                                
                                
                            }
                        }
                        break;
                    case "复制子区":
                        CopyedCompoent = selectedComponet;
                        break;
                    case "删除子区":
                        OnDeleteObj();
                        break;
                    case "Mark属性":
                        break;
                    default://放置元件
                        if(e.ClickedItem.Text.IndexOf("元件") == 0)
                        {
                            int ID = Convert.ToInt32(e.ClickedItem.Text.Substring(2, e.ClickedItem.Text.Length - 2));
                            NodeLib nodeLib = NodeLibManager.Find(ID);
                            if (nodeLib != null)
                            {
                                NodeElement nodeElement = new NodeElement(selectedComponet.GetMaxID() + 1, ucCanvas.CurPosInImg, nodeLib);
                                selectedComponet.AddElement(nodeElement);
                                CreateEleNode(selectedComponet.Tag as TreeNode, nodeElement);
                                ucCanvas.Invalidate();
                            }
                        }
                        break;
                }
            }
            else if(selectedObj is NodeElement)
            {
                NodeElement nodeElement = selectedObj as NodeElement;
                switch (e.ClickedItem.Text)
                {
                    case "添加至元件库":
                        {

                            NodeLib nodeLib = new NodeLib(NodeLibManager.GetMaxID() + 1, nodeElement.Size, nodeElement.Angle);
                            var rect = new Rectangle((int)nodeElement.StartPoint.X, (int)nodeElement.StartPoint.Y, (int)nodeElement.Size.Width, (int)nodeElement.Size.Height);
                            nodeLib.Image = ucCanvas.GetImage(rect, nodeElement.angle);
                            NodeLibManager.nodeLibs.Add(nodeLib);
                            nodeElement.Lib = nodeLib;
                            (nodeElement.Tag as TreeNode).Text = nodeElement.Name;
                        }
                        break;
                    case "更新元件库":
                        {
                            var rect = new Rectangle((int)nodeElement.StartPoint.X, (int)nodeElement.StartPoint.Y, (int)nodeElement.Size.Width, (int)nodeElement.Size.Height);
                            nodeElement.Lib.Image = ucCanvas.GetImage(rect, nodeElement.angle);
                            nodeElement.Lib.size = nodeElement.Size;
                            nodeElement.Lib.Angle = nodeElement.Angle;
                            nodeElement.Lib.UpdateAllElements();
                            ucCanvas.Invalidate();
                        }
                        break;
                    case "删除元件":
                        OnDeleteObj();
                        break;
                    default:
                        {
                            if (e.ClickedItem.Text.IndexOf("元件") == 0)
                            {
                                int ID = Convert.ToInt32(e.ClickedItem.Text.Substring(2, e.ClickedItem.Text.Length - 2));
                                NodeLib nodeLib = NodeLibManager.Find(ID);
                                if (nodeLib != null)
                                {
                                    nodeElement.Lib = nodeLib;
                                    (nodeElement.Tag as TreeNode).Text = nodeElement.Name;
                                    ucCanvas.Invalidate();
                                }
                            }
                        }
                        break;
                }
            }
            else if(selectedObj is NodeMark)
            {
                NodeMark nodeMark = selectedObj as NodeMark;
                switch (e.ClickedItem.Text)
                {
                    case "更新算子":
                        var rect = new Rectangle((int)nodeMark.StartPoint.X, (int)nodeMark.StartPoint.Y, (int)nodeMark.Size.Width, (int)nodeMark.Size.Height);
                        nodeMark.Image = ucCanvas.GetImage(rect, nodeMark.angle);
                        break;
                    case "Mark属性":
                        FormMark formMark = new FormMark(nodeMark);
                        formMark.ShowDialog(this);
                        break;
                    default:break;
                }
            }
            switch(e.ClickedItem.Text)
            {
                case "粘贴子区":
                    if (CopyedCompoent != null)
                    {
                        NodeComponent component = CopyedCompoent.Copy();
                        PointF curPt = ucCanvas.CurPosInImg;
                        component.Move(curPt.X - component.CurPt.Pt.X, curPt.Y - component.CurPt.Pt.Y);
                        component.ID = GetMaxCompID() + 1;
                        Components.Add(component);
                        TreeNode treeNode = CreateCompNode(component);
                        SelectedObj = component;
                    }
                    break;
                default:break;
            }
        }
        #endregion 右键菜单

        int GetMaxCompID()
        {
            int ID = 0;
            if (Components.Count > 0)
            {
                ID = Components.Max(p => p.ID);
            }
            return ID;
        }

        /// <summary>
        /// 创建子区节点
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        TreeNode CreateCompNode(NodeComponent node)
        {
            TreeNode treeNode = new TreeNode(node.Name)
            {
                Name = node.Name,
                Tag = node
            };
            node.Tag = treeNode;
            ucCanvas.AddDrawObject(node);
            tvComponent.Nodes.Add(treeNode);
            foreach (var ele in node.Elements)
            {
                CreateEleNode(treeNode, ele);
            }
            return treeNode;
        }

        /// <summary>
        /// 创建元件节点
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="node"></param>
        void CreateEleNode(TreeNode parent, NodeElement node)
        {
            TreeNode treeNodeElement = new TreeNode(node.Name)
            {
                Name = node.Name,
                Tag = node
            };
            node.Tag = treeNodeElement;
            parent.Nodes.Add(treeNodeElement);
        }

        void UpdateLBPos(CDrawObj obj)
        {
            if (obj is NodeComponent)
            {
                var comp = obj as NodeComponent;
                tbInfo.Text= string.Format("{0}\r\n位置:{1:#.##},{2:#.##}\r\n角度：{3:0.##}°", comp.Name, comp.CurPt.Pt.X, comp.CurPt.Pt.Y, comp.BaseAngle); // = $"{comp.basePt.Pt.X},{comp.basePt.Pt.Y};{comp.BaseAngle}°";
            }
            else if (obj is NodeElement)
            {
                var ele = obj as NodeElement;
                tbInfo.Text = string.Format("{0}\r\n位置:{1:#.##},{2:#.##}\r\n尺寸:{3:#.##},{4:#.##}\r\n角度:{5:0.##}°", ele.Name,
                    ele.StartPoint.X, ele.StartPoint.Y, ele.Size.Width, ele.Size.Height, ele.Angle); //$"{ele.StartPoint.X},{ele.StartPoint.Y};{ele.Size.Width},{ele.Size.Height};{ele.BaseAngle}°";
            }
            else if(obj is NodeMark)
            {
                var ele = obj as NodeMark;
                tbInfo.Text = string.Format("{0}\r\n位置:{1:#.##},{2:#.##}\r\n尺寸:{3:#.##},{4:#.##}\r\n角度:{5:0.##}°", ele.Name,
                    ele.StartPoint.X, ele.StartPoint.Y, ele.Size.Width, ele.Size.Height, ele.Angle); //$"{ele.StartPoint.X},{ele.StartPoint.Y};{ele.Size.Width},{ele.Size.Height};{ele.BaseAngle}°";
            }
            else
                tbInfo.Text = string.Format("坐标:{0:#.##},{1:#.##}", ucCanvas.CurPosInImg.X, ucCanvas.CurPosInImg.Y); 
        }
        CDrawObj TrySelectObj(PointF pos,float scale)
        {

            foreach( var mark in Marks)
            {
                var info = mark.GetTrack(pos, scale);
                if (info.pos != TrackerPos.tOut)
                {
                    return mark;
                }
            }

            foreach (var comp in Components)
            {
                var info = comp.GetTrack(pos, scale);
                if (info.pos != TrackerPos.tOut)
                {
                    return comp;
                }
                foreach (var ele in comp.Elements)
                {
                    info = ele.GetTrack(pos, scale);
                    if (info.pos != TrackerPos.tOut)
                    {
                        return ele;
                    }
                }

            }
            return null;
        }
    }

    /// <summary>
    /// 编辑模式
    /// </summary>
    enum EditMode
    {
        /// <summary>
        /// 无状态
        /// </summary>
        None,
        /// <summary>
        /// 创建元件
        /// </summary>
        CreateComponent,
        /// <summary>
        /// 创建子区
        /// </summary>
        CreateElement,
        /// <summary>
        /// 创建Mark
        /// </summary>
        CreateMark,
        /// <summary>
        /// 平移
        /// </summary>
        Move,
        /// <summary>
        /// 调整大小
        /// </summary>
        Resize,
        /// <summary>
        /// 选中状态
        /// </summary>
        Selected,
    }
}
